package com.iceGame;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

public class OptionsScreen implements Screen {
	OrthographicCamera guiCam;
	SpriteBatch batcher;
	Vector3 touchPoint;
	Rectangle soundBounds;
	Rectangle musicBounds;
	Rectangle signsBounds;
	Rectangle backBounds;
	float xMusicButton;
	float yMusicButton;
	int   musicOn;
	float xSoundButton;
	float ySoundButton;
	int soundOn;
	float xSignsButton;
	float ySignsButton;
	int signsOn;
	Game game;

	
	public OptionsScreen(Game game) {
		this.game = game;
		int width = 800;
		int height = 480;
		batcher = new SpriteBatch();
		guiCam = new OrthographicCamera(width,height);		
		guiCam.position.set(width / 2, height / 2, 0);
		touchPoint = new Vector3();
		configure();	
	}



	private void configure() {
		backBounds = new Rectangle(0,0,64,64);
		musicBounds = new Rectangle(550,217,160,60);
		soundBounds = new Rectangle(550,147,160,60);
		signsBounds = new Rectangle(550,77,160,60);		
		if (Settings.musicEnabled)
			musicOn=1;
		else
			musicOn=0;
		xMusicButton=556+musicOn*musicBounds.width/2;
		yMusicButton=208;
		
		if (Settings.soundEnabled)
			soundOn=1;
		else
			soundOn=0;
		xSoundButton=556+soundOn*soundBounds.width/2;
		ySoundButton=138;
		
		
		if (Settings.signsEnabled)
			signsOn=1;
		else
			signsOn=0;
		xSignsButton=556+signsOn*signsBounds.width/2;
		ySignsButton=68;
		
		
		
	}



	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		Settings.save();

	}
	public void update(float deltaTime){
		if (Gdx.input.justTouched()) {
			guiCam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
			if (OverlapTester.pointInRectangle(musicBounds, touchPoint.x, touchPoint.y)){
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				Settings.musicEnabled=!(Settings.musicEnabled);
				if (Settings.musicEnabled)
					musicOn=1;
				else
				{
					musicOn=0;
				}
				xMusicButton=556+musicOn*musicBounds.width/2;
			}
			else if (OverlapTester.pointInRectangle(soundBounds, touchPoint.x, touchPoint.y)){
					if (Settings.soundEnabled)
						Assets.buttonPressSound.play();	
					Settings.soundEnabled=!(Settings.soundEnabled);
					if (Settings.soundEnabled)
						soundOn=1;
					else
						soundOn=0;
					xSoundButton=556+soundOn*soundBounds.width/2;
				}
			else if (OverlapTester.pointInRectangle(signsBounds, touchPoint.x, touchPoint.y)){
				if (Settings.soundEnabled)
					Assets.buttonPressSound.play();
				Settings.signsEnabled=!(Settings.signsEnabled);
				if (Settings.signsEnabled)
					signsOn=1;
				else
					signsOn=0;
				xSignsButton=556+signsOn*signsBounds.width/2;
			}
				else if	(OverlapTester.pointInRectangle(backBounds, touchPoint.x, touchPoint.y)){
					if (Settings.soundEnabled)
						Assets.buttonPressSound.play();
					game.setScreen(new MainMenuScreen(game));
					Settings.save();
					this.dispose();
			}
			
			
		}
	}

	@Override
	public void render(float deltaTime) {
		update(deltaTime);
		GLCommon gl = Gdx.gl;
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		guiCam.update();
		batcher.setProjectionMatrix(guiCam.combined);	
		batcher.disableBlending();
		batcher.begin();
		batcher.draw(Assets.settingScreen, 0, 0, 800, 480);
		//music button
		batcher.draw(Assets.settingButton,xMusicButton,yMusicButton,musicBounds.width/2,musicBounds.height);
		//sound button
		batcher.draw(Assets.settingButton,xSoundButton,ySoundButton,soundBounds.width/2,soundBounds.height);
		//signs button
		batcher.draw(Assets.settingButton,xSignsButton,ySignsButton,signsBounds.width/2,signsBounds.height);
		batcher.enableBlending();
		batcher.draw(Assets.backButton, backBounds.x, backBounds.y, backBounds.width ,backBounds.height);
		batcher.end();
		batcher.disableBlending();

	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

}
